//
//  GetMailInfo.m
//  ELife
//
//  Created by fenglu on 11-7-23.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "GetMailInfo.h"
#import "GTMBase64.h"
#include "GDataXMLNode.h"

@implementation GetMailInfo
@synthesize user_id;

-(id)init:(id)target
{
    if ([super init]) {
        
    }
    delegate = target;
    
    app = [ELifeAppDelegate App];
    return self;
}


-(void) getMyMailAddress
{
    NSMutableDictionary *param = [[NSMutableDictionary alloc]initWithCapacity:3];
    [param setObject:@"getmailingaddresslist" forKey:@"action"];
    NSString* userid = [GetMyData getUserId];
    [param setObject:userid forKey:@"user_id"];
    [param setObject:[[NSBundle mainBundle] localizedStringForKey:(@"ClientVersion") value:@"" table:nil] forKey:@"version"];
    
    NSString* urlRegister = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] localizedStringForKey:(@"BussinessHost") value:@"" table:nil], @"mailing_address.php?"];
    
    NSDictionary* dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSURL URLWithString:urlRegister], @"iRequestMailAddress:", nil] forKeys:[NSArray arrayWithObjects:@"url", @"requestMethod", nil]];
    
    [[app httpRequestPoint]openUrl:dic postParam:param target:self];
    [param release];

}


#pragma mark -
#pragma mark 联网回调


- (void) iRequestMailAddress:(NSData *)request
{
    NSMutableString* retStr = [[NSMutableString alloc]  initWithData:[GTMBase64 decodeData:request] encoding:NSUTF8StringEncoding];
    NSLog(@"%@", retStr);
    [retStr release];
//    if ([retStr length] == 0) {
        
//    }
    
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[GTMBase64 decodeData:request] options:0 error:nil];
    GDataXMLElement* rootElement = [doc rootElement];
    //    NSLog(@"--------root element:--------\n%@",rootElement);
    NSArray* children = [rootElement children];
    //NSLog(@"--------root's children:--------\n%@", children);
    GDataXMLElement* bodyElement = [[rootElement elementsForName:@"body"]objectAtIndex:0];
    //    NSLog(@"--------body:--------\n%@", bodyElement);
    NSArray* temp = [bodyElement elementsForName:@"mail_list"];
   
    if ([temp count] > 0) {
        GDataXMLElement* goods = [temp objectAtIndex:0];
        NSLog(@"--------goods:--------\n%@", goods);
        GDataXMLElement* item = [[goods elementsForName:@"item"] objectAtIndex:0];
        NSLog(@"--------item:--------\n%@", item);
        NSString* mail_people = [[[item elementsForName:@"mail_people"]objectAtIndex:0] stringValue];
        NSString* mail_id = [[[item elementsForName:@"mail_id"]objectAtIndex:0] stringValue];
        NSString* mail_address = [[[item elementsForName:@"mail_address"]objectAtIndex:0] stringValue];
        NSString* mail_zipcode = [[[item elementsForName:@"mail_zipcode"]objectAtIndex:0] stringValue];
        NSString* mail_phone = [[[item elementsForName:@"mail_phone"]objectAtIndex:0] stringValue];
        NSDictionary* dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:mail_people, mail_id, mail_address, mail_zipcode, mail_phone, nil] forKeys:[NSArray arrayWithObjects:@"mail_people", @"mail_id", @"mail_address", @"mail_zipcode", @"mail_phone", nil]];
        
        NSLog(@"%@", dic);
        
        NSArray* tempArray = [NSArray arrayWithObjects:dic, nil];
        [delegate getMailFinish:tempArray];
    }else
    {
        [delegate NotMailAddress];
    }
    
}


- (void)iRequestFailed
{
    [app finishLoginView];
}

-(void)dealloc
{
    [user_id release];
    [super dealloc];
    
}

@end
